//
//  ICEEMGetRiskData.m
//  EasyMoney
//
//  Created by 邓乐 on 2020/1/9.
//  Copyright © 2020 zh. All rights reserved.
//

#import "ICEEMGetRiskData.h"
#import "EMRiskDataModel.h"
#import "EMLocationManager.h"
#import "ICEJavaScriptBridge.h"
#import "EMContactsUtil.h"

@implementation ICEEMGetRiskData

+ (instancetype)sharedSingleton {
    static ICEEMGetRiskData *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //不能再使用alloc方法
        //因为已经重写了allocWithZone方法，所以这里要调用父类的分配空间的方法
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

// 防止外部调用alloc 或者 new
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [ICEEMGetRiskData sharedSingleton];
}

// 防止外部调用copy
- (id)copyWithZone:(nullable NSZone *)zone {
    return [ICEEMGetRiskData sharedSingleton];
}

// 防止外部调用mutableCopy
- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [ICEEMGetRiskData sharedSingleton];
}

- (void)getRiskData:(NSDictionary *)args
{
    BOOL needContacts = [(NSNumber *)[args objectForKey:@"contacts"] boolValue];
    EMRiskDataModel *riskData = [EMRiskDataModel rickData];
    NSMutableDictionary* ret = [NSMutableDictionary dictionary];
     [ret setObject:@"success" forKey:@"status"];
     if (needContacts) {
         [[EMContactsUtil sharedInstance] getUserContacts:^(NSMutableArray<EMContactsModel *> *contacts) {
             riskData.contactInfo = contacts;
             [ret setObject:riskData.yy_modelToJSONObject forKey:@"riskData"];
             [ICEJavaScriptBridge sendReslutToJS:ret callInfo:args];
         } failed:^(NSError *error, CNAuthorizationStatus status) {
             [ret setObject:riskData.yy_modelToJSONObject forKey:@"riskData"];
             [ICEJavaScriptBridge sendReslutToJS:ret callInfo:args];
         }];
     } else {
         [ret setObject:riskData.yy_modelToJSONObject forKey:@"riskData"];
         [ICEJavaScriptBridge sendReslutToJS:ret callInfo:args];
     }
}



@end
